﻿unit SysManageService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SKSOFTComm,
  SysManageModel,
  SysManageBLL;
  
type

  TSysManageService = class(TSysManageBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
  {$ENDIF}

    function LoadAll(const iItems:TSysManageList; out oError:string):Boolean;
    function Add(const iItem:TSysManage; out oError:string):Boolean;
    function GetManageFileStream(const iManageId:string; var iFileStream:TMemoryStream; out oError:string):Boolean;

    class function GetFileVersion(const iFileName:string):string;
    class function GetFileSize(const iFileName:string):LongInt;
    class function VersionCompare(const iNewVersion,iOldVersion:string):Integer;
  end;
  
implementation

{$IFDEF SERVER}
function TSysManageService.BeforeTreatment(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.BeforeTreatment(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.BeforeCheck(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.BeforeCheck(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.Check(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.Check(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.BeforeExecute(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.BeforeExecute(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.AfterTreatment(const iItem:TSysManage; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TSysManageService.AfterTreatment(const iItems:TSysManageList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;
{$ENDIF}

function TSysManageService.LoadAll(const iItems:TSysManageList; out oError:string):Boolean;
var
  Condition,OrderBy:string;
begin
  if (iItems<>nil) then
  begin
    Condition:='1=1';
    OrderBy:=TSysManageModel.cManageSortNoKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TSysManageService.LoadAll方法失败！错误内容:传入的iItems为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysManageService.Add(const iItem:TSysManage; out oError:string):Boolean;
var
  SQL:string;
  MaxSortNo,NewSortNo:Integer;
  Cds:TClientDataSet;
begin
  if (iItem<>nil) then
  begin
    Cds:=TClientDataSet.Create(nil);
    try
      SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s;'),
              [TSysManageModel.cManageSortNoKey,TSysManageModel.cManageSortNoKey,
              TSysManageModel.cTblName]);
      if TSKSOFTComm.GetCDS(fServConn,EmptyStr,SQL,Cds,oError) then
      begin
        if Cds.FieldByName(TSysManageModel.cManageSortNoKey).IsNull then
        begin
          MaxSortNo:=1;
        end else begin
          MaxSortNo:=Cds.FieldByName(TSysManageModel.cManageSortNoKey).AsInteger;
        end;
        NewSortNo:=MaxSortNo+1;
      end else begin
        oError:='调用TSysManageService.Add方法失败,错误内容:获取最大SortNo失败!';
        Result:=False;
        Exit;
      end;
    finally
      Cds.Free;
    end;

    iItem.ManageSortNo:=NewSortNo;
    iItem.TimeStamp:=TSKSOFTComm.GetTimeStampMs;
    if not inherited Add(iItem,oError) then
    begin
      oError:=Format(string('调用TSysManageService.Add方法失败！错误内容:%s'),[oError]);
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用TSysManageService.Add方法失败！错误内容:传入的iItem为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TSysManageService.GetManageFileStream(const iManageId:string; var iFileStream:TMemoryStream; out oError:string):Boolean;
var
  SQL:string;
  Cds:TClientDataSet;
begin
  if (not SameStr(iManageId,EmptyStr)) and (iFileStream<>nil)then
  begin
    SQL:=Format(string('select %s from %s where %s=''%s'' '),
                [TSysManageModel.cManageFileStrKey,TSysManageModel.cTblName,
                TSysManageModel.cManageIdKey,iManageId]);
    Cds:=TClientDataSet.Create(nil);
    try
      if TSKSOFTComm.GetCDS(fDBConn,EmptyStr,SQL,Cds,oError) then
      begin
        if Cds.RecordCount>0 then
        begin
          TSKSOFTComm.Base64StrToMemStream(Cds.FieldByName(TSysManageModel.cManageFileStrKey).AsAnsiString,
                              iFileStream);
        end else begin
          iFileStream.Clear;
        end;
      end else begin
        oError:=Format(string('调用TSysManageService.GetManageFileStream方法失败！错误内容:%s!'),[oError]);
        Result:=False;
        Exit;
      end;
    finally
      Cds.Free;
    end;
  end else begin
    oError:=Format(string('调用TSysManageService.GetManageFileStream方法失败！错误内容:传入的iManageId或iFileStream为空!'),[]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

class function TSysManageService.GetFileVersion(const iFileName: string):string;
type
   PVerInfo = ^TVS_FIXEDFILEINFO;
   TVS_FIXEDFILEINFO = record
      dwSignature: longint;
      dwStrucVersion: longint;
      dwFileVersionMS: longint;
      dwFileVersionLS: longint;
      dwFileFlagsMask: longint;
      dwFileFlags: longint;
      dwFileOS: longint;
      dwFileType: longint;
      dwFileSubtype: longint;
      dwFileDateMS: longint;
      dwFileDateLS: longint;
   end;
var
    ExeNames: array[0..255] of char;
    VerInfo: PVerInfo;
    Buf: pointer;
    Sz: word;
    L, Len: Cardinal;
begin
    StrPCopy(ExeNames, iFileName);
    Sz := GetFileVersionInfoSize(ExeNames, L);
    if Sz=0 then
       begin
          Result:='0.0.0.0';
          Exit;
       end;
    try
      GetMem(Buf, Sz);
      try
        GetFileVersionInfo(ExeNames, 0, Sz, Buf);
        if VerQueryValue(Buf, '\', Pointer(VerInfo), Len) then
           begin
             Result := IntToStr(HIWORD(VerInfo.dwFileVersionMS)) + '.' +
                       IntToStr(LOWORD(VerInfo.dwFileVersionMS)) + '.' +
                       IntToStr(HIWORD(VerInfo.dwFileVersionLS)) + '.' +
                       IntToStr(LOWORD(VerInfo.dwFileVersionLS));
           end
        else
           result:='0.0.0.0';
      finally
         FreeMem(Buf);
      end;
    except
       Result := '0.0.0.0';
    end;
end;

class function TSysManageService.GetFileSize(const iFileName:string):LongInt;
var SearchRec: TSearchRec;
begin
  if FindFirst(ExpandFileName(iFileName), faAnyFile, SearchRec) = 0 then
   Result := SearchRec.Size
  else
   Result := -1;
end;

class function TSysManageService.VersionCompare(const iNewVersion,iOldVersion:string):Integer;
var
  NewVersionList,OldVersionList:TStrings;
  Ops,NewVersionNum,OldVersionNum:Integer;
begin
  NewVersionList:=TStringList.Create;
  OldVersionList:=TStringList.Create;
  try
    NewVersionList.Delimiter:='.';
    NewVersionList.DelimitedText:=iNewVersion;
    OldVersionList.Delimiter:='.';
    OldVersionList.DelimitedText:=iOldVersion;
    for Ops := 0 to StrToInt(IfThen(NewVersionList.Count>OldVersionList.Count,IntToStr(NewVersionList.Count),IntToStr(OldVersionList.Count)))-1 do
    begin
      if (Ops>=NewVersionList.Count)
        or SameStr(Trim(NewVersionList.Strings[Ops]),EmptyStr) then
      begin
        NewVersionNum:=0;
      end else begin
        NewVersionNum:=StrToInt(NewVersionList.Strings[Ops]);
      end;
      if (Ops>=OldVersionList.Count)
        or SameStr(Trim(OldVersionList.Strings[Ops]),EmptyStr) then
      begin
        OldVersionNum:=0;
      end else begin
        OldVersionNum:=StrToInt(OldVersionList.Strings[Ops]);
      end;

      if NewVersionNum>OldVersionNum then
      begin
        Result:=1;
        Exit;
      end else if NewVersionNum<OldVersionNum then
      begin
        Result:=-1;
        Exit;
      end;
    end;
  finally
    NewVersionList.Free;
    OldVersionList.Free;
  end;
  Result:=0;
end;
  
end.
